package com.wufeng.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wufeng.dto.R;
import com.wufeng.entity.Identity;
import com.wufeng.service.IdentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("identity")
public class IdentityController {


    @Autowired
    private IdentityService identityService;

    /**
     * 根据身份证号查出身份信息
     *
     * @param idCard
     * @return
     */
    @GetMapping("/getIdentityByIdCard")
    public R getIdentityByIdCard(@RequestParam("idcard") String idCard) {
        QueryWrapper<Identity> qw = new QueryWrapper<Identity>().eq("idCard", idCard);
        Identity identity = identityService.getOne(qw);
        HashMap<String, Object> map = new HashMap<>();
        map.put("identity", identity);
        return R.ok(map);
    }


    /**
     * 添加或更新身份信息
     *
     * @param identity
     * @return
     */
    @PostMapping("/addOrUpdateIdentity")
    public R addOrUpdateIdentity(@RequestBody Identity identity) {

        QueryWrapper<Identity> qw = new QueryWrapper<>();


        identity.setUploadtime(new Date());
        try {
            identityService.saveOrUpdate(identity,qw);
        } catch (Exception e) {
            return R.error("操作失败!");
        }
        return R.ok("操作成功！");
    }


}
